package demo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @description:
 * @author: GX
 * @time: 2025/11/1 10:28
 */
public class Reflection {
    public static void main(String[] args) throws Exception{
        User user = User.class.getDeclaredConstructor().newInstance();
        user.setName("高鑫");
        user.setAge(20);
        Field declaredField = User.class.getDeclaredField("name");
        declaredField.setAccessible(true);
        declaredField.set(user,"北京");
        System.out.println(user.getName());
//        MemoryTest obj1 = new MemoryTest();
//        MemoryTest obj2 = new MemoryTest();
//
//        // 实例方法调用 - 需要对象
//        obj1.showInstanceMethod();
//        obj2.showInstanceMethod();
//
//        // 静态方法调用 - 直接通过类名
//        MemoryTest.showStaticMethod();
//
//        // 证明方法字节码只有一份
//        System.out.println("obj1的方法: " + obj1.showInstanceMethod());
//        System.out.println("obj2的方法: " + obj2.showInstanceMethod());
//        System.out.println("静态方法: " + MemoryTest.showStaticMethod());
    }

    private static void test1() throws ClassNotFoundException {
        User user = new User();
        user.setName("高鑫");
        user.setAge(20);
        System.out.println(user.getName());
        System.out.println(user.getAge());
        Class<?> StringClazz = Class.forName("java.lang.String");

        //已知类全限定名获取类信息
        Class<?> clazz = Class.forName("demo.User");
        //已有实例，获取实例的类信息
        Class<?> userClazz = user.getClass();
        String name = userClazz.getName();      //获取类包路径
        System.out.println(name);

        System.out.println("===========================");
        //获取类的父类的类信息
        Class<?> superclass = userClazz.getSuperclass();
        System.out.println(superclass.getName());         //输出父类的全限定名

        System.out.println("============interfaces================");
        //获取类的接口信息
        Class<?>[] interfaces = StringClazz.getInterfaces();
        for(Class<?> i : interfaces){
            System.out.println(i.getName());
        }
        System.out.println("=============constructors================");
        //获取类的构造器信息
        Constructor<?>[] constructors = StringClazz.getConstructors();
        for(Constructor<?> c : constructors ){
            System.out.println(c);
        }

        System.out.println("============methods===============");
        //获取类的方法
        Method[] methods = userClazz.getMethods();
        Method[] declaredMethods = userClazz.getDeclaredMethods();
        for(Method m : methods){
            System.out.println(m.getName());
        }
        System.out.println("==========declaredMethods==================");
        for(Method m : declaredMethods){
            System.out.println(m.getName());
        }
    }
}
class MemoryTest {
    private String instanceField = "实例字段";
    private static String staticField = "静态字段";

    public String showInstanceMethod() {
        System.out.println("实例方法 - 可以访问:");
        System.out.println("实例字段: " + instanceField);
        System.out.println("静态字段: " + staticField);
        return "";
    }

    public static String showStaticMethod() {
        System.out.println("静态方法 - 可以访问:");
        // System.out.println("实例字段: " + instanceField); // ❌ 编译错误
        System.out.println("静态字段: " + staticField);
        return "";
    }
}